import time

import allure
from selenium.webdriver.common.by import By

from Base.AppBase.baseApp import BasePage, BaseHandle


# 黑马app 的首页对象库层
class LoginPage(BasePage):
    def __init__(self):
        super().__init__()

        self.login_text = By.XPATH, "//android.view.View/*[@contains(@text,'登录')]"
        self.mobile = By.XPATH, "//*[@index='1']"
        self.code = By.XPATH, "//*[@index='2']"
        self.login_button = By.CLASS_NAME, "android.widget.Button"

    def find_login_text(self):
        return self.get_element(self.find_login_text())

    def find_mobile(self):
        return self.get_element(self.mobile)

    def find_code(self):
        return self.get_element(self.code)

    def find_login_button(self):
        return self.get_element(self.login_button)


# 黑马app的操作层
class LoginHandle(BaseHandle):
    def __init__(self):
        self.login_page = LoginPage()

    @allure.step(title="获取用户名文本")
    def get_login_text(self):
        return self.login_page.find_login_text().text

    @allure.step(title="输入用户名")
    def send_mobile(self, mobile):
        self.input_text(self.login_page.find_mobile(), mobile)

    @allure.step(title="输入验证码")
    def send_code(self, code):
        self.input_text(self.login_page.find_code(), code)

    @allure.step(title="点击登录")
    def click_login_button(self):
        self.login_page.find_login_button().click()


# 黑马app的业务层
class LoginProxy:
    def __init__(self):
        self.login_handle = LoginHandle()


    def is_login_exit(self):
        if self.login_handle.get_login_text():
            return False

    def go_login(self, mobile, code):

        time.sleep(3)

        self.login_handle.send_mobile(mobile)
        time.sleep(3)
        self.login_handle.send_code(code)
        time.sleep(3)
        self.login_handle.click_login_button()

